#!/bin/bash
# 在Docker容器内提取ABI（使用GNU工具）
# 因为macOS的nm/readelf工具功能有限，在容器内使用完整的GNU binutils

set -e

LIBRARY_DIR="/workspace/data/build/libraries"
OUTPUT_DIR="/workspace/output/abi_data"
EXTRACTOR="/workspace/src/extractor/abi_extractor.py"

# 安装Python依赖
echo "[INFO] 安装Python依赖..."
pip3 install --break-system-packages pandas openpyxl -q 2>&1 | tail -n 5 || true

# 创建输出目录
mkdir -p "$OUTPUT_DIR"

# 批量提取
echo "[INFO] 开始批量提取ABI..."
total=0
success=0

for lib in "$LIBRARY_DIR"/*.so*; do
    # 跳过符号链接
    if [ -L "$lib" ]; then
        continue
    fi
    
    lib_name=$(basename "$lib")
    output_file="$OUTPUT_DIR/${lib_name}.abi.xlsx"
    
    ((total++))
    echo "[INFO] [$total] 提取 $lib_name ..."
    
    if python3 "$EXTRACTOR" --library "$lib" --output "$output_file" 2>&1 | grep -E "SUCCESS|ERROR"; then
        ((success++))
    fi
    
    echo ""
done

echo "========================================"
echo "提取完成！"
echo "总计: $total 个库"
echo "成功: $success 个"
echo "输出目录: $OUTPUT_DIR"
echo "========================================"

